package com.xgluo;

import com.xgluo.jframe.SelectPathFile;

import java.io.*;

/**
 * @author xgluo
 * @program: makePackage
 * @name: ReadPath
 * @description: 读取  需要打包的文件路径   的文件
 * @date 2025年02月26日
 */
public class ReadPath {

    public static void main(String[] args) {
        SelectPathFile selectPathFile = new SelectPathFile();
        /*url = fileTest.open();*/
        String url = selectPathFile.openFileDialog();
        if (url == null ||"".endsWith(url)) {
            return;
        }
        ReadConfigFile instance = ReadConfigFile.getInstance(url);
        System.out.println("projectPath: "+instance.getProjectPath());
        System.out.println("classPath: "+instance.getClassPath());
        System.out.println("webcontextPath: "+instance.getWebcontextPath());
        System.out.println("----------------------------------------");
        /*url = fileTest.open();*/
        url = selectPathFile.openDialog();
        if (url == null ||"".endsWith(url)) {
            return;
        }
        File selectFile = new File(url);
        if(selectFile.isDirectory()) {
            String[] textFileUrls = selectFile.list((dir, name) -> name.endsWith(".txt"));
            if(textFileUrls != null) {
                for (String textFileUrl : textFileUrls) {
                    readPathFile(textFileUrl);
                }
            }
        }else{
            readPathFile(url);
        }

        String projectPath = System.getProperty("user.dir");
        System.out.println(projectPath);
        System.out.println("文件读取完成！");

    }


    /**
     * @description: 按行读取文件内容，获取需要上线的文件路径
     * @param path 待处理的路径文件
     * @date 2025/2/26
     * @author xgluo
     */
    private static void readPathFile(String path) {
        // 创建对象用于处理文件
        CopyFiles copyFiles = new CopyFiles();
        // 读取路径文件中的待处理文件路径
        try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
            String line;
            while ((line = reader.readLine()) != null) {
                if(line.isEmpty()) {
                    continue;
                }
                copyFiles.copyFiles(line);
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println(e.getMessage()+":"+path);
            e.printStackTrace();
        }
    }


}
